package com.songlanyun.modules.supplychainapi.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.map.MapUtil;
import com.songlanyun.common.annotation.AuthIgnore;
import com.songlanyun.common.supplychainapi.goods.model.GoodsPageResult;
import com.songlanyun.common.supplychainapi.order.ApiOrderCommon;
import com.songlanyun.common.supplychainapi.order.model.*;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.R;
import com.songlanyun.modules.user.controller.AbstractController;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("admin/supplychain/order")
public class SupplyChainOrderAdminController extends AbstractController {

    @Resource
    private ApiOrderCommon apiOrderCommon;


    /**
     * 错误订单详情
     */
    @GetMapping("listErrorInfo/{id}")
    public R listErrorInfo(@PathVariable("id") String id) {
        ListErrorInfo listErrorInfo = apiOrderCommon.listErrorInfo(id);
        return R.ok().put("data", listErrorInfo);
    }

    /**
     * 订单错误列表【最新】
     */
    @GetMapping("listErrorNew")
    public R listErrorNew(@RequestParam Map<String, Object> params) {
        ListErrorNew listErrorNew = apiOrderCommon.listErrorNew(params);
        List<ListErrorNew.List> list = CollectionUtil.list(false, listErrorNew.getList());
        return R.ok().put("page", new PageUtils(list, listErrorNew.getTotal(),
                MapUtil.getInt(params, "limit", 10),
                MapUtil.getInt(params, "page", 1)));
    }

    /**
     * 可售检测V2
     */
    @GetMapping("availableCheck")
    public R availableCheck(@RequestBody CheckParam checkParam) {
        AvailableCheck availableCheck = apiOrderCommon.availableCheck(checkParam);
        return R.ok().put("data", availableCheck);
    }

    /**
     * 前置校验V2
     */
    @PostMapping("/beforeCheck")
    public R beforeCheck(@RequestBody CheckParam checkParam) {
        BeforeCheck[] beforeChecks = apiOrderCommon.beforeCheck(checkParam);
        return R.ok().put("data", beforeChecks);
    }


    /**
     * 全平台订单列表V2
     */
    @GetMapping("/orderList")
    public R orderList(@RequestParam Map<String, Object> params) {
        OrderList orderList = apiOrderCommon.orderList(params);
        List<OrderList.List> list = CollectionUtil.list(false, orderList.getList());
        return R.ok().put("page", new PageUtils(list, orderList.getTotal(),
                MapUtil.getInt(params, "limit", 10),
                MapUtil.getInt(params, "page", 1)));
    }

    /**
     * 各平台订单列表V2
     */
    @GetMapping("/orderSourceList/{source}")
    public R orderSourceList(@PathVariable("source") Integer source) {
        OrderSourceList orderSourceList = apiOrderCommon.orderSourceList(source);
        return R.ok().put("data", orderSourceList);
    }

}
